package cn.appsys.service.developer.AppInfo;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.appsys.dao.developer.AppInfo.AppInfoMapper;
import cn.appsys.dao.developer.AppVersion.AppVersionMapper;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;

@Transactional
@Service("AppInfoService")
public class AppInfoServiceImpl implements AppInfoService {

	@Autowired
	AppInfoMapper appInfoMapper;
	@Autowired
	AppVersionMapper appVersionMapper;

	@Override
	public int appInfoCount(AppInfo app) {
		// TODO Auto-generated method stub
		return appInfoMapper.getAppInfoCount(app);
	}

	@Override
	public List<AppInfo> appInfoList(AppInfo app) {
		// TODO Auto-generated method stub
		return appInfoMapper.getAppInfoList(app);
	}

	@Override
	public AppInfo appInfoAPKName(String APKName) {
		// TODO Auto-generated method stub
		return appInfoMapper.getAppInfoAPKName(APKName);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean appInfoadd(AppInfo appInfo) {
		boolean YesNo = false;
		int result = appInfoMapper.getAppInfoadd(appInfo);
		if(result == 1){
			YesNo = true;
		}
		return YesNo;
	}

	@Override
	public AppInfo appInfoById(Integer id) {
		// TODO Auto-generated method stub
		return appInfoMapper.getAppInfoById(id);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean appInfoBylogoPicPathDel(Integer id) {
		// TODO Auto-generated method stub
		boolean YesNo = false;
		int result = appInfoMapper.getAppInfoBylogoPicPathDel(id);
		if(result == 1){
			YesNo = true;
		}
		return YesNo;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean appInfomodify(AppInfo appInfo) {
		boolean YesNo = false;
		int result = appInfoMapper.getAppInfomodify(appInfo);
		if(result == 1){
			YesNo = true;
		}
		return YesNo;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean appInfodel(Integer id) {
		boolean YesNo = false;
		int result = appInfoMapper.getAppInfodel(id);
		if(result == 1){
			YesNo = true;
		}
		return YesNo;
	}

	@Override
	public boolean appsysUpdateSaleStatusByAppId(AppInfo appInfoObj) throws Exception {
		// TODO Auto-generated method stub
		AppInfo appInfo = appInfoMapper.getAppInfoById(appInfoObj.getId());
		Integer operator = appInfoObj.getModifyBy();
		if(operator < 0 || appInfoObj.getId() < 0){
			throw new Exception();
		}
		if(null == appInfo){
			return false;
		}else{
			switch(appInfo.getStatus()){
			case 2:
				onSale(appInfo,operator,4,2);
				break;
			case 5:
				onSale(appInfo,operator,4,2);
				break;
			case 4:
				offSale(appInfo,operator,5);
				break;
			default:
				return false;
			}
		}
		return true;
	}

	private void onSale(AppInfo appInfo,Integer operator,Integer appInfStatus,Integer versionStatus) throws Exception{
		offSale(appInfo,operator,appInfStatus);
		setSaleSwitchToAppVersion(appInfo,operator,versionStatus);
	}



	private boolean offSale(AppInfo appInfo, Integer operator, Integer appInfStatus) {
		// TODO Auto-generated method stub
		AppInfo _appInfo = new AppInfo();
		_appInfo.setId(appInfo.getId());
		_appInfo.setStatus(appInfStatus);
		_appInfo.setModifyBy(operator);
		_appInfo.setOffSaleDate(new Date(System.currentTimeMillis()));
		appInfoMapper.getAppInfomodify(_appInfo);
		return true;
	}

	private boolean setSaleSwitchToAppVersion(AppInfo appInfo, Integer operator, Integer versionStatus) {
		AppVersion appVersion = new AppVersion();
		appVersion.setId(appInfo.getVersionId());
		appVersion.setPublishStatus(versionStatus);
		appVersion.setModifyBy(operator);
		appVersion.setModifyDate(new Date(System.currentTimeMillis()));
		appVersionMapper.getAppVersionmodify(appVersion);
		return true;
	}


}
